#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

inline bool run(int y) { 
  return ((y % 4 == 0 and y % 100 != 0) or y % 400 == 0);
}

long long dom[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
long long add[] = {0, 31, 28, 31, 30, 31, 30, 177}, cnt = 1, ans = 0;

int yy, mm, dd;
ll tt = 0, now;

int main() { 
  cin >> yy >> mm >> dd >> tt;
  int y = yy, m = mm, d = dd;
  
  int by = 1900, bm = 1, bd = 1, bw = 1;
  while (by != y or bm != m or bd != d) { 
    bw++;
    if (bw == 8) bw = 1;
    bd++;
    if (bd == dom[bm] + 1) 
      bd = 1, bm++;
    if (bm == 12 + 1) 
      bm = 1, by++;
    if (run(by)) 
      dom[2] = 29;
    else 
      dom[2] = 28;
  }
  int w = bw, ww = w;
  while (now++, tt-- >= 1) { 
    if (m == d) { 
      if (d == w and ww == w) 
        ans++;
      now += add[d];
      ww += add[d] % 7;
      if (ww == 0) ww = 7;
      if (mm == 7) { 
        mm = 12, dd = 31;
      } else 
        m++;
    }
    dd++;
    ww++;
    ww %= 7;
    if (ww == 0) ww = 7;
    if (dd == dom[mm] + 1) 
      dd = 1, mm++;
    if (mm == 13)
      mm = 1, yy++;
  }
  cout << ans;
  return 0;



  
  for (long long y = 1900, w = 1; y <= 2300; y++) { 
    if ((y % 4 == 0 and y % 100 != 0) or y % 400 == 0) 
      dom[2]++, add[2]++;
    for (long long m = 1; m <= 12; m++) { 
      for (long long d = 1; d <= dom[m]; d++, w++, cnt++) { 
        w %= 7;
        if (not w) w = 7;
        if (m == d) {
          if (d == w and w == 2) { 
            printf("%lld\t%lld\t%lld\t%lld\n", y, m, d, w);
            ans++;
          }
          cnt += add[d];
          w += add[d];
          w %= 7;
          if (not w) w = 7;
          if (m == 7) { 
            m = 12, d = 31;
          } else m++;
        }
      }
    }
    if ((y % 4 == 0 and y % 100 != 0) or y % 400 == 0) 
      dom[2]--, add[2]--;
  }
  cout << cnt;
}
